_Pragma("once");

#include <string>
#include <netinet/in.h>

#include "common.h"
#include "NetworkType.h"
#include "Log.h"
#include "ProcessCreator.h"

/**
 * @author lizb
 */
class INetwork {
public:
    INetwork();
    virtual ~INetwork() ;


    static constexpr char STOP[] = "stop";
    static constexpr char START[] = "start";

    static constexpr char STOP_HARDWARE[] = "stopHardware";
    static constexpr char START_HARDWARE[] = "startHardware";


    void retrieveInterfaceInfo();

    static short getIterfaceFlags(const std::string name, int socketfd);

    static int setIterfaceFlags(const std::string name, int socketfd, short flags);

    static int setIterfaceAddressParameter(const std::string name, int socketfd, unsigned long request, sockaddr_in& addr);

    static bool running(short flags);

    virtual bool running() {
        short flags = getIterfaceFlags(mname, msocketFd);
        return running(flags);
    }

    void active();


    virtual void stop() {
        const char * args[] = {nullptr, nullptr, mname.data(), STOP, nullptr};
        mprocessCreator.runScript(NETWORK_MANAGE_SCRIPT, args);
    }

    virtual void start() {
        const char * args[] = {nullptr, nullptr, mname.data(), START, nullptr};
        mprocessCreator.runScript(NETWORK_MANAGE_SCRIPT, args);
    }



    virtual void stopHardware() {
        const char * args[] = {nullptr, nullptr, mname.data(), STOP_HARDWARE, nullptr};
        mprocessCreator.runScript(NETWORK_MANAGE_SCRIPT, args);
    }

    virtual void startHardware() {
        const char * args[] = {nullptr, nullptr, mname.data(), START_HARDWARE, nullptr};
        mprocessCreator.runScript(NETWORK_MANAGE_SCRIPT, args);
    }


    virtual std::string getSignalStrength() {
        return "n/a";
    }


    virtual bool canSwitch() {
        return true;
    }


    /**
     * 连接名称 NetworkInterface的名字
     */
    std::string mname;
    /**
     * 连接的类型 有线 无线 光纤等
     * NETWORK_4G NETWORK_ENTE
     */
    NetworkType mtype;
    /**
     * 网关地址
     */
    std::string mgateway;

    /**
     * 接口地址
     */
    std::string minterfaceAddress;
    std::string mmacAddress;

    int msocketFd;

    ProcessCreator& mprocessCreator;
};
